var baseUrl = require('../../utils/urllink.js')
import { AreaPicker } from "../areaSelector/selector.js"
var app = getApp()
Page(Object.assign({}, AreaPicker, {
  data: {
    showTopTips: false,
    showTopTipsText: '',
    disabled:false,
    files: [],
    images: [], //上传的图片地址 *
    hostimgs: app.globalData.hostimgs,

    goods_name: '',//  *
    goods_kuanhao: '',
    goods_remark: '',//  *
    cost_price: 0,
    shop_price: 0,//  *
    market_price: 0,//  *
    store_count: 0,//  *
    currentItem: 0, //品牌id *
    goods_category: [],//商品分类
    goods_categoryid: 0,//商品分类id，3级
    pingpai: [],
    fenggelist: [],//风格list
    shop_zpingpai: '',//商品自定义品牌
    shop_zyanse: '',//商品自定义规格 颜色
    fenggecurrentid:0,  //风格id
    xuanyanse: {},//选择的颜色值id（多选数组）
    xuanchima: {},//选择尺码的值id（多选数组）
    xuanyanse_list: [], //选择的颜色name（多选数组）
    xuanchima_list: [], //选择尺码的name（多选数组）
    shuxing_list: [],//属性集合
    xuanshuxing_list: {},//选择的属性list  *
    shuxing_list_name: '',
    sppp: '',
    spshuxinginput: '',

    currentItem1: 0,
    currentItem2: 0,
    currentItem3: 0,

    is_show_guige: false,
    is_show_shuxing: false,
    is_show_chima: false,
    is_show_pingpai: false,
    is_show_fengge: false,
    showModal: false,//模拟框 属性
    showyingxiao:false,//提示框
    guigelist: [],////动态规格列表添加 *
    chima1: [],
    chima2: [],
    chima3: [],

    store_id: 0,
    goods_id: 0,

    province: null,//省 {id,name}
    city: null,//市
    district: null,//县 区
    street: null,//村 街道 
    info: [],
    spec: [],
    brand_id: 0,

  },

  //添加用户选择地区完成的回调
  onAreaCommit(locationList) {
    console.log(locationList);
    var that = this
    this.setData({
      province: locationList[0] || {},
      city: locationList[1] || {},
      district: locationList[2] || {},
      street: locationList[3] || {},
    });

    //显示添加属性
    this.setData({
      is_show_shuxing: true,
      goods_categoryid: locationList[1].id,
      is_show_guige: true,
    });
    //获取商品风格
    goods_tags1(that, locationList[1].id) 
  },
  //点击 风格事件
  onclickfengge: function (e) {
    var id = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    this.setData({
      fenggecurrentid: id,
    })

  },
  //提示弹出框事件
  showyingxiao: function () {
    this.setData({
      showyingxiao: !this.data.showyingxiao,

    });
  },
  //初始化
  onLoad: function (options) {
    var that = this
    console.log(options)
    console.log("传参goods_id=" + options.goods_id)
    this.setData({
      images: [],
      goods_id: options.goods_id,
    })




    //商品分类调用数据 pid=0
    //getgoods_category(that, 0)

    //查询一个商品
    get_goods(that, that.data.goods_id)

  },

  //隐藏品牌
  yingcangpp: function (e) {
    var that = this;
    that.setData({
      is_show_pingpai: false,
    })
  },
  //发布商品提交
  formSubmit(e) {
    var that = this;
    var goods_name = e.detail.value.goods_name;//商品名称 
    var goods_kuanhao = e.detail.value.goods_kuanhao;//商品款号
    var goods_remark = e.detail.value.goods_remark;//商品款号
    
    var cost_price = parseFloat(e.detail.value.cost_price) || 0 ;
    var shop_price = parseFloat(e.detail.value.shop_price) || 0 ;//本店价
    var market_price = parseFloat(e.detail.value.market_price) || 0 ;//
    var store_count = parseFloat(e.detail.value.store_count);//总库存
    var fenggecurrentid = this.data.fenggecurrentid;//风格 fenggecurrentid
    var images = this.data.images;//上传文件
    var currentItem = this.data.currentItem;  //品牌id
    var goods_categoryid = this.data.goods_categoryid;//分类3级id
    var shuxing_list = this.data.shuxing_list;//属性集合
    var guigelist = this.data.guigelist;//规格集合

    if (goods_name.length <= 0) {
      //错误提示
      this.setData({
        showTopTipsText: '请输入商品名称',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else if (goods_remark.length <= 0) {
      //错误提示
      this.setData({
        showTopTipsText: '请输入商品简介',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else if (goods_kuanhao.length <= 0) {
      //错误提示
      this.setData({
        showTopTipsText: '请输入款号',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else if (cost_price == 0) {
      //错误提示
      this.setData({
        showTopTipsText: '请输入批发价',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else if (shop_price == 0) {
      //错误提示
      this.setData({
        showTopTipsText: '请输入零售价格',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else if (market_price == 0) {
      //错误提示
      this.setData({
        showTopTipsText: '请输入吊牌价格',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else if (shop_price < cost_price) {
      //错误提示
      this.setData({
        showTopTipsText: '零售价必须大于批发价',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else if (market_price < shop_price) {
      console.log(market_price)
      console.log(shop_price)
      //错误提示
      this.setData({
        showTopTipsText: '吊牌价必须大于等于零售价',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else if (store_count.length <= 0) {
      //错误提示
      this.setData({
        showTopTipsText: '请输入商品库存',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else if (currentItem == 0) {
      //错误提示
      this.setData({
        showTopTipsText: '请选择一个品牌',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else if (fenggecurrentid == 0) {
      //错误提示
      this.setData({
        showTopTipsText: '请选择一个商品风格',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else if (images.length <= 0) {
      //错误提示
      this.setData({
        showTopTipsText: '请上传图片',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else {

      //编辑提交商品调用接口
      modi_goods(that, that.data.goods_id, goods_categoryid, goods_name, goods_kuanhao, goods_remark, cost_price, market_price, shop_price, this.data.store_id, store_count, guigelist, shuxing_list, currentItem, images, fenggecurrentid)
    }





  },





  //多选  选择颜色
  checkboxChange(e) {

    var values = e.detail.value
    var checkbox = this.data.chima3
    console.log(e.detail.value)
    var clist = []
    var c = 0
    for (let i = 0, lenI = checkbox.length; i < lenI; ++i) {
      checkbox[i].checked = !1
      for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (checkbox[i].id == values[j]) {
          checkbox[i].checked = !0
          var clist1 = {};
          clist1['id'] = checkbox[i].id;
          clist1['name'] = checkbox[i].item;
          clist[c] = clist1;
          c++;
          break
        }
      }
    }

    this.setData({
      chima3: checkbox,
      xuanyanse: values,
      xuanyanse_list: clist,
    })
    console.log("选择的颜色ID");
    console.log(this.data.xuanyanse_list);
    console.log(this.data.xuanchima_list);



    //动态规格列表添加
    if (this.data.xuanyanse_list.length > 0 && this.data.xuanchima_list.length > 0) {
      this.setData({
        is_show_chima: true,
      })
      //guigelist 列表中添加值
      //js 组合集合值到guigelist中
      var guigelist = []

      var k = 0
      for (var i = 0; i < this.data.xuanyanse_list.length; i++) {

        for (var j = 0; j < this.data.xuanchima_list.length; j++) {
          var guigelist1 = {}
          guigelist1['ysid'] = this.data.xuanyanse_list[i].id;
          guigelist1['ysname'] = this.data.xuanyanse_list[i].name;
          guigelist1['cmid'] = this.data.xuanchima_list[j].id;
          guigelist1['cmname'] = this.data.xuanchima_list[j].name;
          guigelist1['jiage'] = 'jiage' + k;
          guigelist1['jiage_value'] = this.data.shop_price;
          guigelist1['kc'] = 'kc' + k;
          guigelist1['kc_value'] = 0;
          for(var q=0;q<this.data.guigelist.length; q++){
            if ((this.data.xuanyanse_list[i].id == this.data.guigelist[q].ysid) && (this.data.xuanchima_list[j].id == this.data.guigelist[q].cmid)){
              guigelist1['kc_value'] = this.data.guigelist[q].kc_value ;
            }
          }

          guigelist1['ggid'] = k + 1;
          guigelist[k] = guigelist1;
          k++;
        }

      }
      this.setData({
        guigelist: guigelist,
      })
      console.log(guigelist)
    } else {
      this.setData({
        is_show_chima: false,
        guigelist: [],
      })
    }

  },

  //多选  选择尺码
  checkboxChange2(e) {

    var values = e.detail.value
    var checkbox = this.data.chima2
    console.log(e.detail.value)
    var clist = []
    var c = 0
    for (let i = 0, lenI = checkbox.length; i < lenI; ++i) {
      checkbox[i].checked = !1
      for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (checkbox[i].id == values[j]) {
          checkbox[i].checked = !0
          var clist1 = {};
          clist1['id'] = checkbox[i].id;
          clist1['name'] = checkbox[i].item;
          clist[c] = clist1;
          c++;
          break
        }
      }
    }

    this.setData({
      chima2: checkbox,
      xuanchima: values,
      xuanchima_list: clist,
    })
    console.log("选择的尺码ID" + this.data.xuanchima);
    console.log(this.data.xuanchima[0]);

    console.log("xuanchima_list="+this.data.xuanchima_list.length);
    console.log("xuanyanse_list="+this.data.xuanyanse_list.length);
    //动态规格列表添加
    if (this.data.xuanyanse_list.length > 0 && this.data.xuanchima_list.length > 0) {
      this.setData({
        is_show_chima: true,
      })
      //guigelist 列表中添加值
      //js 组合集合值到guigelist中
      var guigelist = []

      var k = 0
      for (var i = 0; i < this.data.xuanyanse_list.length; i++) {

        for (var j = 0; j < this.data.xuanchima_list.length; j++) {
          var guigelist1 = {}
          guigelist1['ysid'] = this.data.xuanyanse_list[i].id;
          guigelist1['ysname'] = this.data.xuanyanse_list[i].name;
          guigelist1['cmid'] = this.data.xuanchima_list[j].id;
          guigelist1['cmname'] = this.data.xuanchima_list[j].name;
          guigelist1['jiage'] = 'jiage' + k;
          guigelist1['jiage_value'] = this.data.shop_price;
          guigelist1['kc'] = 'kc' + k;
          guigelist1['kc_value'] = 0;
          for (var q = 0; q < this.data.guigelist.length; q++) {
            if ((this.data.xuanyanse_list[i].id == this.data.guigelist[q].ysid) && (this.data.xuanchima_list[j].id == this.data.guigelist[q].cmid)) {
              guigelist1['kc_value'] = this.data.guigelist[q].kc_value;
            }
          }
          guigelist1['ggid'] = k + 1;
          guigelist[k] = guigelist1;
          k++;
        }

      }
      this.setData({
        guigelist: guigelist,
      })
      console.log(guigelist)
    } else {
      this.setData({
        is_show_chima: false,
        guigelist: [],
      })
    }

  },
  //规格列表的价格 输入框失去焦点时触发事件
  onclickguigelistjiage: function (e) {
    var values = e.detail.value
    console.log(values)
    var id = e.currentTarget.dataset.id
    console.log(id)
    var guigelist = this.data.guigelist;
    for (var i = 0; i < guigelist.length; i++) {
      if (id == guigelist[i].ggid) {
        guigelist[i].jiage_value = values;

      }
    }
    this.setData({
      guigelist: guigelist,
    })
    console.log(this.data.guigelist)
  },
  //规格列表的库存 输入框失去焦点时触发事件
  onclickguigelistkc: function (e) {
    var values = e.detail.value
    console.log(values)
    var id = e.currentTarget.dataset.id
    console.log(id)
    var guigelist = this.data.guigelist;
    for (var i = 0; i < guigelist.length; i++) {
      if (id == guigelist[i].ggid) {
        guigelist[i].kc_value = values;
      }
    }
    this.setData({
      guigelist: guigelist,
    })
    console.log(this.data.guigelist)
  },
  // 属性的 多选框事件 
  checkboxChange_shuxin: function (e) {
    var values = e.detail.value
    var id = e.currentTarget.dataset.id
    console.log(values)

    var cklist = this.data.shuxing_list

    for (var i = 0; i < cklist[id].values.length; i++) {
      cklist[id].values[i].checked = !1;
      for (var j = 0; j < values.length; j++) {
        if (cklist[id].values[i].name == values[j]) {
          cklist[id].values[i].checked = !0;
        }
      }
    }
    this.setData({
      shuxing_list: cklist,
    })
  },
  // 属性的 单选框事件 
  checkboxChange_shuxin2: function (e) {
    var values = e.detail.value
    var id = e.currentTarget.dataset.id
    var cklist = this.data.shuxing_list
    for (var i = 0; i < cklist[id].values.length; i++) {
      if (cklist[id].values[i].name == values) {
        cklist[id].values[i].checked = true;
      } else {
        cklist[id].values[i].checked = false;
      }

    }

    this.setData({
      shuxing_list: cklist,
    })

  },
  //上传图片 显示功能
  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      sizeType: 'compressed', // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        console.log(res.tempFilePaths)
        that.setData({
          files: res.tempFilePaths,
          images: [],//清空图片数据
          disabled: true,
        });
        //上传图片 递归方法
        sendPhotos(res.tempFilePaths, that, app.globalData.openid, app.globalData.user_id)

      }
    })


  },
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.dataset.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },

  //弹出模拟框事件 打开 属性
  toggleModal() {

    this.setData({
      showModal: true,

    });
  },
  //弹出模拟框事件 保存 属性
  toggleModalsave: function (e) {
    var that = this
    that.setData({
      showModal: false,

    });

  },
  //弹出模拟框事件 取消 属性
  toggleModalquxiao: function (e) {
    var that = this
    that.setData({
      showModal: false,

    });
  },

  //添加规格
  showguige: function () {
    var that = this;
    // 返回规格
    var chima2 = that.data.chima2;
    var chima3 = that.data.chima3;
    var spec = that.data.spec;




    if (that.data.is_show_guige == false) {
      // 标记集合中选中的颜色和尺码
      if (spec.length > 0) {
        for (var i = 0; i < chima2.length; i++) {
          for (var j = 0; j < spec.length; j++) {
            if (spec[j].sizeid == chima2[i].id) {
              chima2[i].checked = true;//集合添加元素
              break
            }
          }
        }
        var chima3 = that.data.chima3;
        for (var i = 0; i < chima3.length; i++) {
          for (var j = 0; j < spec.length; j++) {
            if (spec[j].colorid == chima3[i].id) {
              chima3[i].checked = true;//集合添加元素
              break
            }
          }
        }
        var xuanyanse_list = that.data.xuanyanse_list;
        var xuanchima_list = that.data.xuanchima_list;
        var k = 0; var s = 0;
        for (var i = 0; i < chima2.length; i++) {
          if (chima2[i].checked == true) {
            var xuanchima_list1 = {}
            xuanchima_list1['id'] = chima2[i].id;
            xuanchima_list1['name'] = chima2[i].item;
            xuanchima_list[k] = xuanchima_list1;
            k++;
          }
        }
        for (var i = 0; i < chima3.length; i++) {
          if (chima3[i].checked == true) {
            var xuanyanse_list1 = {}
            xuanyanse_list1['id'] = chima3[i].id;
            xuanyanse_list1['name'] = chima3[i].item;
            xuanyanse_list[s] = xuanyanse_list1;
            s++;
          }
        }
        // 数据存到集合中
        var guigelist = that.data.guigelist;
        for (var i = 0; i < spec.length; i++) {
          var guigelist1 = {}
          guigelist1['ysid'] = spec[i].colorid;
          guigelist1['ysname'] = spec[i].color_name;
          guigelist1['cmid'] = spec[i].sizeid;
          guigelist1['cmname'] = spec[i].size_name;
          guigelist1['jiage'] = 'jiage' + i;
          guigelist1['jiage_value'] = spec[i].price;
          guigelist1['kc'] = 'kc' + i;
          guigelist1['kc_value'] = spec[i].store_count;
          guigelist1['ggid'] = i + 1;
          guigelist[i] = guigelist1;
        }

        that.setData({
          xuanyanse_list: xuanyanse_list,
          xuanchima_list: xuanchima_list,
          chima2: chima2,
          chima3: chima3,
          guigelist: guigelist,
          is_show_chima: true,
        })
      }
    }


    that.setData({
      is_show_guige: true,
    })


  },
  //添加品牌
  showpingpai: function () {
    var that = this;
    that.setData({
      currentItem: that.data.brand_id,
    })
    that.setData({
      is_show_pingpai: true,
    })
  },
  //弹出框 滚动到底部触发事件
  upper: function (e) {

    wx.showToast({
      title: '到底了',
      icon: 'success',
      duration: 1000
    })
  },

  //点击 品牌事件
  onclickpingpai: function (e) {
    var id = e.currentTarget.dataset.id;
    this.setData({
      currentItem: id,
      shop_zpingpai: '',
    })
    console.log(id)

  },
  //本店售价 文本框输入事件
  onclickshop_price: function (e) {
    console.log(e.detail.value);
    var that = this;
    that.setData({
      shop_price: e.detail.value
    })
  },
  // 批发价
  onclickcost_price: function (e) {
    console.log(e.detail.value);
    var that = this;
    that.setData({
      cost_price: e.detail.value
    })
  },
  //自定义商品品牌事件
  onclickinputpingpai: function (e) {
    console.log(e.detail.value);
    var that = this;
    that.setData({
      shop_zpingpai: e.detail.value,
      currentItem: 0,
    })
  },
  //自定义规格颜色 输入框
  onclickinputzyanse: function (e) {
    console.log(e.detail.value);
    var that = this;
    that.setData({
      shop_zyanse: e.detail.value,
      currentItem3: 0,
    })
  },
  //批量添加价格
  onclickguigelistpljg: function (e) {
    var pljiage = e.detail.value
    var guigelist = this.data.guigelist;
    for (var i = 0; i < guigelist.length; i++) {
      guigelist[i].jiage_value = pljiage
    }
    this.setData({
      guigelist: guigelist,
    })
  },
  //批量添加库存
  onclickguigelistplkc: function (e) {
    var plkc = e.detail.value
    var guigelist = this.data.guigelist;
    for (var i = 0; i < guigelist.length; i++) {
      guigelist[i].kc_value = plkc
    }
    this.setData({
      guigelist: guigelist,
    })
  },
  //自定义规格颜色 输入框 提交
  onclickyanse: function (e) {
    var that = this;
    if (that.data.shop_zyanse.length <= 0) {
      console.log("提交颜色没值")
      //错误提示
      this.setData({
        showTopTipsText: '请输入自定义颜色',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else {
      //提交自定义颜色到数据库
      //请求服务器
      wx.request({
        url: baseUrl +'weapp/Goods/add_goods_color.html',

        data: {
          store_id: that.data.store_id,//当前店铺ID
          color_name: that.data.shop_zyanse,//颜色名称
        },
        method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        header: {
          'content-type': 'application/json'
        }, // 设置请求的 header
        success: function (res) {
          console.log(res.data)
          var yanse = res.data;
          for (var i = 0; i < yanse.length; i++) {
            yanse[i].checked = !1//集合添加元素
          }
          var chima3 = that.data.chima3;
          chima3.push(yanse)
          that.setData({
            chima3: chima3,
            shop_zyanse: '',
          })

        },
        fail: function () {
        },
        complete: function () {
          // complete
        }
      })


    }
  },
  //自定义品牌提交
  onclicksavepingpai: function (e) {
    var that = this;
    if (that.data.shop_zpingpai.length <= 0) {
      console.log("提交品牌没值")
      //错误提示
      this.setData({
        showTopTipsText: '请输入自定义品牌',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else {
      //提交自定义颜色到数据库
      //请求服务器
      wx.request({
        url: baseUrl +'weapp/Goods/add_store_brand.html',

        data: {
          brand_name: that.data.shop_zpingpai,
          store_id: that.data.store_id,

        },
        method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        header: {
          'content-type': 'application/json'
        }, // 设置请求的 header
        success: function (res) {
          console.log(res.data)
          var pingpai = that.data.pingpai
          pingpai.push(res.data);
          that.setData({
            pingpai: pingpai,
            shop_zpingpai: '',
          })
        },
        fail: function () {
        },
        complete: function () {
          // complete
        }
      })


    }
  },

  //自定义品牌删除
  onclickdelpingpai: function (e) {
    var that = this;
    if (that.data.shop_zpingpai.length <= 0) {
      console.log("提交品牌没值")
      //错误提示
      this.setData({
        showTopTipsText: '请输入自定义品牌',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else {
      wx.showModal({
        title: '提示',
        content: '您确定要删除该品牌吗',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
            //提交自定义颜色到数据库
            //请求服务器
            wx.request({
              url: baseUrl +'weapp/Goods/delete_brand.html',

              data: {
                brand_name: that.data.shop_zpingpai,
                store_id: that.data.store_id,

              },
              method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
              header: {
                'content-type': 'application/json'
              }, // 设置请求的 header
              success: function (res) {
                console.log(res.data)
                if (res.data.code == 0) {
                  console.log("删除品牌成功")
                  
                  var pingpai = that.data.pingpai
                  for (var i = 0; i < pingpai.length; i++) {
                    if (pingpai[i].name == that.data.shop_zpingpai) {
                      pingpai.splice(i, 1);
                      i=0;
                    }
                  }
                  that.setData({
                    pingpai: pingpai,
                    shop_zpingpai: '',
                  })
                } else {
                  console.log("删除品牌失败")
                  wx.showModal({
                    title: '提示',
                    content: '输入错误，没有该品牌',
                    showCancel: false,
                    success: function (res) {
                      if (res.confirm) {
                        console.log('用户点击确定')
                      } else if (res.cancel) {
                        console.log('用户点击取消')
                      }
                    }
                  })
                }

              },
              fail: function () {
              },
              complete: function () {
                // complete
              }
            })

          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })



    }
  },



}))


//上传图片
// function sendPhotos(arr, that, index, openid, user_id) {
//   wx.showToast({
//     icon: "loading",
//     title: "正在上传"
//   })
//   wx.uploadFile({
//     url: baseUrl +'weapp/SavePicApi/save_shoppic.html',
//     filePath: arr,
//     name: 'file',
//     formData: {
//       //和服务器约定的token, 一般也可以放在header中
//       'openid': openid,
//       'user_id': user_id,
//       'index':index,
//     },
//     header: { "Content-Type": "multipart/form-data" },
//     success: function (res) {
//       console.log(res.data)
//       //console.log(JSON.parse(res.data));
//       //var images = JSON.parse(res.data);
//       var images = that.data.images;
//       images.push(JSON.parse(res.data));
//       that.setData({
//         images: images,
//       })
//     }
//   })
// }

//上传图片 递归方法
function sendPhotos(arr, that,openid, user_id) {


  if (arr.length != 0) {
    wx.showToast({
      icon: "loading",
      title: "正在上传"
    })
    wx.uploadFile({
      url: baseUrl + 'weapp/SavePicApi/save_shoppic.html',
      filePath: arr[0],
      name: 'file',
      formData: {
        //和服务器约定的token, 一般也可以放在header中
        'openid': openid,
        'user_id': user_id,
      },
      success: function (res) {
        var data = res.data
        console.log(res.data)
        var images = that.data.images;
        images.push(JSON.parse(res.data));
        that.setData({
          images: images,
        })
        arr.splice(0, 1)
        sendPhotos(arr, that, openid, user_id)
      }
    })
  }else{
    that.setData({
      disabled: false,
    })
  }
}

//获取品牌数据
function getpingpai(that, store_id) {
  //请求服务器
  wx.request({
    url: baseUrl +'weapp/Goods/store_brand.html',

    data: {
      store_id: store_id,
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      that.setData({
        pingpai: res.data
      })

    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })
}
//获取商品尺码规格：（包含尺寸 尺码 颜色等） 参数：cate_id => 商品的顶级分类 如，男装女装鞋子 store_id => 当前店铺的ID  type     => 1:尺寸,2:尺码,3:颜色
function getguige(that, cate_id, store_id, type) {
  //请求服务器
  wx.request({
    url: baseUrl +'weapp/Goods/goods_size.html',

    data: {
      cate_id: cate_id,
      store_id: store_id,
      type: type,
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {

      if (type == 1) {
        that.setData({
          chima1: res.data.list,
        })
      } else if (type == 2) {
        var chima2 = res.data.list;
        for (var i = 0; i < chima2.length; i++) {
          chima2[i].checked = !1//集合添加元素
        }
        that.setData({
          chima2: chima2,
        })

      } else if (type == 3) {
        var chima3 = res.data.list;
        for (var i = 0; i < chima3.length; i++) {
          chima3[i].checked = !1//集合添加元素
        }
        that.setData({
          chima3: chima3,
        })
      } else { }
      //显示规格数据
      shouguige(that)

    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })
}

//获取商品颜色
function goods_color(that, cate_id, store_id) {
  //请求服务器
  wx.request({
    url: baseUrl +'weapp/Goods/goods_color.html',

    data: {
      cate_id: cate_id,
      store_id: store_id,
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)


      var chima3 = res.data.list;
      for (var i = 0; i < chima3.length; i++) {
        chima3[i].checked = !1//集合添加元素
      }
      that.setData({
        chima3: chima3,
      })
      //显示规格数据
      shouguige(that)


    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })
}

//获取商品分类
function getgoods_category(that, pid) {
  //请求服务器
  wx.request({
    url: baseUrl +'weapp/Goods/goods_category.html',

    data: {
      pid: pid,
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      that.setData({
        goods_category: res.data
      })

    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })
}

//获取商品属性 cate_id => 最小分类ID store_id => 店铺ID
function getgoods_attr(that, cate_id, store_id) {
  //请求服务器
  wx.request({
    url: baseUrl +'weapp/Goods/goods_attr.html',

    data: {
      cate_id: cate_id,
      store_id: store_id
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log("属性返回值")
      console.log(res.data)
      that.setData({
        shuxing_list: res.data,
      })

    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })
}


/** 发布商品
 * 参数：
        cat_id3 => 最小分类ID
        goods_name => 商品名称
        market_price => 市场价
        shop_price => 店铺价
        store_id => 店铺ID
        store_count => 总库存字段：
        guigelist => 规格集合
        xuanshuxing_list => 属性集合
        currentItem => 品牌id
        images => 图片集合
 */
//发布商品
function add_goods(that, cat_id3, goods_name, goods_kuanhao, goods_remark, cost_price, market_price, shop_price, store_id, store_count, guigelist, shuxing_list, currentItem, images, fenggecurrentid) {
console.log(goods_remark)
  //请求服务器
  wx.request({
    url: baseUrl +'weapp/Goods/add_goods.html',

    data: {
      cat_id3: cat_id3,
      goods_name: goods_name,
      spu: goods_kuanhao,
      goods_remark: goods_remark,
      market_price: market_price,
      shop_price: shop_price,
      store_id: store_id,
      store_count: store_count,
      guigelist: guigelist,
      shuxing_list: shuxing_list,
      currentItem: currentItem,
      tags: fenggecurrentid,
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log("发布商品返回值")
      console.log(res.data)
      //成功提示

    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })
}

//查询一个商品
function get_goods(that, goods_id) {

  //请求服务器
  wx.request({
    url: baseUrl +'weapp/Goods/get_goods_detail_by_ucenter.html',

    data: {
      goods_id: goods_id,

    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log("获取商品返回值")
      console.log(res.data)




      // 返回图片
      var files = that.data.files
      var images = that.data.images
      for (var i = 0; i < res.data.images.length; i++) {
        files.push(that.data.hostimgs + res.data.images[i].image_url);
        images.push(that.data.hostimgs + res.data.images[i].image_url);
      }
      // 分类
      var province = {};
      province.id = res.data.info.cat_id1;
      province.name = res.data.info.first_name;
      var city = {};
      city.id = res.data.info.cat_id2;
      city.name = res.data.info.second_name;
      var district = {};
      district.id = res.data.info.cat_id3;
      district.name = res.data.info.third_name;
      //console.log("-------------------22222222-------------------")
      //console.log(res.data.ta)
      //风格
      if (res.data.info.tags > 0){
        that.setData({
          is_show_fengge:true,
        })
      }
      that.setData({
        images: images,
        files: files,
        info: res.data.info,
        fenggecurrentid: res.data.info.tags,
        spec: res.data.spec,
        goods_name: res.data.info.goods_name,
        goods_remark: res.data.info.goods_remark,
        goods_kuanhao: res.data.info.spu,
        goods_id: res.data.info.goods_id,
        cost_price: res.data.info.cost_price,
        market_price: res.data.info.market_price,
        shop_price: res.data.info.shop_price,
        store_count: res.data.info.store_count,
        store_id: res.data.info.store_id,
        goods_categoryid: res.data.info.cat_id3,//分类3
        brand_id: res.data.info.brand_id,//品牌id
        currentItem: res.data.info.brand_id,
        //还需要 分类的名称   属性的值
        province: province,//分类名 1级
        city: city,//分类名 2级
        district: district,//分类名 3级
        is_show_shuxing: true,
        shuxing_list: res.data.attr,
      })

      
      //调用品牌数据
      getpingpai(that, res.data.info.store_id)
      //获取商品尺码规格
      getguige(that, res.data.info.cat_id3, res.data.info.store_id, 2)
      //获取商品规格颜色
      goods_color(that, res.data.info.cat_id3, res.data.info.store_id)
      //获取商品属性
      //getgoods_attr(that, res.data.info.cat_id3, res.data.info.store_id)
      //获取商品风格
      goods_tags(that, res.data.info.cat_id3) 

    },
    fail: function () {
    },
    complete: function () {
      // complete

    }
  })
}

//编辑提交商品
function modi_goods(that, goods_id, cat_id3, goods_name, goods_kuanhao, goods_remark, cost_price, market_price, shop_price, store_id, store_count, guigelist, shuxing_list, currentItem, images, fenggecurrentid) {

  //请求服务器
  wx.request({
    url: baseUrl +'weapp/Goods/modi_goods.html',

    data: {
      goods_id, goods_id,
      cat_id3: cat_id3,
      goods_name: goods_name,
      spu:goods_kuanhao,
      goods_remark: goods_remark,
      cost_price: cost_price,
      market_price: market_price,
      shop_price: shop_price,
      store_id: store_id,
      store_count: store_count,
      guigelist: guigelist,
      shuxing_list: shuxing_list,
      currentItem: currentItem,
      images: images,
      tags: fenggecurrentid,
    },
    method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log("发布商品返回值")
      console.log(res.data)
      //成功提示
      if (res.data.code == 0) {
        wx.showModal({
          title: '提示',
          content: '编辑成功',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.redirectTo({
                url: '../goods/goodslist?store_id=' + store_id,
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      } else if (res.data.goods_id > 0) {
        wx.showModal({
          title: '提示',
          content: '编辑成功',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
              
              wx.redirectTo({
                url: '../goods/goodslist?store_id=' + store_id,
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }

    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })
}

//显示规格数据
function shouguige(that) {

  // 返回规格
  var chima2 = that.data.chima2; //尺码
  var chima3 = that.data.chima3; //颜色
  var spec = that.data.spec; // 返回的规格列表数据





  // 标记集合中选中的颜色和尺码
  if (spec.length > 0) {
    for (var i = 0; i < chima2.length; i++) {
      for (var j = 0; j < spec.length; j++) {
        if (spec[j].sizeid == chima2[i].id) {
          chima2[i].checked = true;//集合添加元素
          break
        }
      }
    }
    var chima3 = that.data.chima3;
    for (var i = 0; i < chima3.length; i++) {
      for (var j = 0; j < spec.length; j++) {
        if (spec[j].colorid == chima3[i].id) {
          chima3[i].checked = true;//集合添加元素
          break
        }
      }
    }
    var xuanyanse_list = that.data.xuanyanse_list;
    var xuanchima_list = that.data.xuanchima_list;
    console.log("yanse=" + xuanyanse_list.length)
    console.log("chima=" + xuanchima_list.length)
    var k = 0; var s = 0;
    for (var i = 0; i < chima2.length; i++) {
      if (chima2[i].checked == true) {
        var xuanchima_list1 = {}
        xuanchima_list1['id'] = chima2[i].id;
        xuanchima_list1['name'] = chima2[i].item;
        xuanchima_list[k] = xuanchima_list1;
        k++;
      }
    }
    for (var i = 0; i < chima3.length; i++) {
      if (chima3[i].checked == true) {
        var xuanyanse_list1 = {}
        xuanyanse_list1['id'] = chima3[i].id;
        xuanyanse_list1['name'] = chima3[i].item;
        xuanyanse_list[s] = xuanyanse_list1;
        s++;
      }
    }
    // 数据存到集合中
    var guigelist = that.data.guigelist;
    for (var i = 0; i < spec.length; i++) {
      var guigelist1 = {}
      guigelist1['ysid'] = spec[i].colorid;
      guigelist1['ysname'] = spec[i].color_name;
      guigelist1['cmid'] = spec[i].sizeid;
      guigelist1['cmname'] = spec[i].size_name;
      guigelist1['jiage'] = 'jiage' + i;
      guigelist1['jiage_value'] = spec[i].price;
      guigelist1['kc'] = 'kc' + i;
      guigelist1['kc_value'] = spec[i].store_count;
      guigelist1['ggid'] = i + 1;
      guigelist[i] = guigelist1;
    }

    that.setData({
      xuanyanse_list: xuanyanse_list,
      xuanchima_list: xuanchima_list,
      chima2: chima2,
      chima3: chima3,
      guigelist: guigelist,
      is_show_chima: true,
    })
  }



  that.setData({
    is_show_guige: true,
  })


}

//获取风格list
function goods_tags(that, cat_id3) {
  //请求服务器
  wx.request({
    url: baseUrl + 'weapp/Goods/goods_tags.html',
    data: {
      cat_id3: cat_id3,
    },
    method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      if (res.data.code == 0) {
        that.setData({
          fenggelist: res.data.list,
        })
      }else if(res.data.code == -1){
        that.setData({
          fenggelist: [],
          fenggecurrentid:-1,

        })

      }
      
    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })
}
//获取风格list
function goods_tags1(that, cat_id3) {
  //请求服务器
  wx.request({
    url: baseUrl + 'weapp/Goods/goods_tags.html',
    data: {
      cat_id3: cat_id3,
    },
    method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      if (res.data.code == 0) {
        that.setData({
          fenggelist: res.data.list,
          fenggecurrentid: 0,
          is_show_fengge: true,
        })
      } else if (res.data.code == -1) {
        that.setData({
          fenggelist: [],
          fenggecurrentid: -1,
          is_show_fengge:false,
        })

      }

    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })
}